package com.bandit.controller;

import com.bandit.pojo.Blog;
import com.bandit.service.BlogService;
import com.bandit.service.BlogTypeService;
import com.bandit.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    LoginService loginService;

    @Autowired
    BlogTypeService blogTypeService;

    @Autowired
    BlogService blogService;

    //登录信息确认
    @RequestMapping("/loginConfirm")
    public ModelAndView login(ModelAndView mv,
                              @RequestParam("username") String username,
                              @RequestParam("password") String password,
                              HttpSession session) throws ServletException, IOException {
        System.out.println("DEBUG：enter==>loginConfirm");
        boolean flag = loginService.loginCheck(username, password);
        if (flag){
            int currentPage = 1;
            mv.addObject("pageInfo",blogService.getBlogByPage(currentPage));
            mv.addObject("blogList", blogService.getBlogByPage(currentPage).getBlogList());
            mv.setViewName("show");
            session.setAttribute("LoginUser",username);
        }else {
            mv.setViewName("login");
        }
        return mv;
    }


}
